package com.qiyu.tech.pay.queue;

import com.alibaba.fastjson.JSON;
import com.qiyu.core.cache.redis.client.RedisService;
import com.qiyu.tech.pay.queue.model.DelayTaskInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 支付的每一次任务都会被记录到这张map中
 *
 * @Author linhao
 * @Date created in 9:08 上午 2021/4/2
 */
@Component
public class DelayTaskInfoManager {

    @Resource
    private RedisService redisService;

    private static final String DELAY_TASK_INFO_MAP_KEY = "delay:task:info:map";

    public boolean put(long taskId, DelayTaskInfo delayTaskInfo) {
        return redisService.setMapItem(DELAY_TASK_INFO_MAP_KEY, String.valueOf(taskId), JSON.toJSONString(delayTaskInfo));
    }

    public DelayTaskInfo get(long taskId) {
        String json = (String) redisService.getMapField(DELAY_TASK_INFO_MAP_KEY, String.valueOf(taskId), String.class);
        DelayTaskInfo delayTaskInfo = JSON.parseObject(json, DelayTaskInfo.class);
        return delayTaskInfo;
    }

    /**
     * 完成支付任务
     */
    public void finishPayNotifyJob(DelayTaskInfo delayTaskInfo) {
        redisService.deleteMapItem(DELAY_TASK_INFO_MAP_KEY, String.valueOf(delayTaskInfo.getTaskId()));
    }
}
